$(function () {
    const $searchDropDownBox = $('.search-info-list');
    // 页面热门推荐缓存
    let search_list = []
    let city_obj;
    let choiceSearchMenu;

    // 发起网络请求获取页面初始化信息
    FK_ajax.get(HouseApi.index_info)
        .then((res) => {
            city_obj = res.curLocation
            Load_current_coordinates(city_obj.city);
            Load_Search_Fields(res.searchMenus)
        })
    // 加载当前地址
    function Load_current_coordinates(city) {
        $('.top-left .address').text(city);
    }

    // 加载搜索框字段
    function Load_Search_Fields(searchMenus){
        for(let i in searchMenus){
            if(i==0){
                choiceSearchMenu = searchMenus[i].key;
                $('.search-header ul').append(
                    `<li class="active" data-key="${searchMenus[i].key}">${searchMenus[i].title}</li>`
                );
                continue;
            }
            $('.search-header ul').append(
                `<li data-key="${searchMenus[i].key}">${searchMenus[i].title}</li>`
            );
        }
    }

    $('.search-header ul').on('click','li',function(){
        choiceSearchMenu = $(this).data('key');
        $(this).addClass('active').siblings().removeClass();
    })

    // 加载下拉框和数据
    function downBoxUpdate(search_list, isSearch) {
        // 当搜索框聚焦的时候，显示下拉框
        $searchDropDownBox.css('display', "block")
        // 每次搜索，都清空原本热门搜索提示框的子元素
        $('.search-lists').empty()
        // 添加热门搜索首栏标题
        if (!isSearch) {
            $('.search-lists').append(
                `<li class="hot">热门搜索</li>`
            );
            // 循环遍历数值生成DOM节点，添加到列表中
            for (let item of search_list) {
                $('.search-lists').append(
                    $(document.createElement('li')).text(item.app_house_title)
                );
            }
        } else {
            if (search_list) {
                for (let item of search_list) {
                    $('.search-lists').append(
                        $(document.createElement('li')).append(item.hlsText)
                    );
                }
            } else {
                $('.search-lists').append('<li>暂无内容</li>');
            }

        }
    }

    // 监听ipt聚焦，显示下拉框
    $('.search-input').on('focus', function () {
        if(!$(this).val()){
            if (search_list.length) {
                downBoxUpdate(search_list)
            } else {
                FK_ajax.get(HouseApi.hot_search)
                    .then((res) => {
                        // 获取网络请求的数据源
                        search_list = res.rent_house_list.list
                        downBoxUpdate(search_list)
                    })
            }
        }else{
            FK_ajax.get(HouseApi.search_house, {
                cityId: city_obj.cityCode || 440100,
                cityName: city_obj.city || '广州',
                channel: choiceSearchMenu,
                keyword: $(this).val(),
                query: $(this).val()
            }).then((res) => {
                    downBoxUpdate(res.data.result, true)
                })
        }
    })

    // 监听ipt取消聚焦，隐藏下拉框
    $('.search-input').on('blur', function () {
        // 当搜索框不聚焦的时候，隐藏下拉框
        $searchDropDownBox.css('display', "none")
    })

    // 监听ipt输入文本
    $('.search-input').on('input', function () {
        let _this = this
        if(!$(this).val().trim()){
            if (search_list.length) {
                downBoxUpdate(search_list)
            } else {
                FK_ajax.get(HouseApi.hot_search)
                    .then((res) => {
                        // 获取网络请求的数据源
                        search_list = res.rent_house_list.list
                        downBoxUpdate(search_list)
                    })
            }
            return;
        }
        防抖(function () {
            FK_ajax.get(HouseApi.search_house, {
                cityId: city_obj.cityCode || 440100,
                cityName: city_obj.city || '广州',
                channel: 'site',
                keyword: $(_this).val(),
                query: $(_this).val()
            })
                .then((res) => {
                    downBoxUpdate(res.data.result, true)
                })
        }, 500)
    })

    // 防抖
    let timer;
    function 防抖(fn, timeout) {
        timer && clearTimeout(timer);
        timer = setTimeout(function () {
            fn();
        }, timeout)
    }
})